<?php
class Job extends AppModel {
	var $name = 'Job';
	var $validate = array(
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'title' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'workfield' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	var $actsAs = array('Containable');
	
	function get_jobs($filter) {
		if ($_SESSION['Auth']['User']['group_id'] == 2) {
			$jobs = $this->findMy('all');
			$i = 0;
			foreach($jobs as $job) {
				$profile = $this->User->ContractorProfile->findMy('first');
				$jobs[$i] = array_merge($job, $profile);
				$i++;
			}
			return $jobs;
		} else {
			if($filter == 'job'){
				$conditions = array('Job.type' => $filter);
			} elseif($filter == 'internship') {
				$conditions = array('Job.type' => $filter);
			} else {
				$conditions = array('Job.type' => 'job');
			}
			$jobs = $this->find('all', array('conditions' => $conditions));
			$i = 0;
			foreach($jobs as $job) {
				$profile = $this->User->ContractorProfile->find('first', array('conditions' => array('ContractorProfile.user_id' => $job['Job']['user_id'])));
				$jobs[$i] = array_merge($job, $profile);
				$i++;
			}
			return $jobs;
		}
	}
	
	
	
	function get_job($id = null) {
		if ($id == null) {
			return false;
		} else {
			if ($_SESSION['Auth']['User']['group_id'] == 2){
				if($job = $this->findMy('first', array('conditions' => array('Job.id' => $id)))){
					$profile = $this->User->ContractorProfile->findMy('first');
					$job = array_merge($job, $profile);
				} else {
					return false;
				}
			} else {
				$job = $this->read(null, $id);
				$profile = $this->User->ContractorProfile->find('first', array('conditions' => array('ContractorProfile.user_id' => $job['Job']['user_id'])));
				$job = array_merge($job, $profile);
			}
			return $job;
		}
	}
	
	function get_random() {
		$this->recursive = 3;
		$data = $this->find('all', array('contain' => array('User'), 'fields' => array('Job.title', 'Job.contract', 'Job.workfield','Job.description', 'Job.id')));
		$i = 0;
		foreach($data as $job) {
			$avatar = $this->User->ContractorProfile->find('first', array('conditions' => array('ContractorProfile.user_id' => $job['User']['id']), 'fields' => array('ContractorProfile.avatar', 'ContractorProfile.company')));
			$data[$i]['ContractorProfile']['avatar'] = $avatar['ContractorProfile']['avatar'];
			$data[$i]['ContractorProfile']['company'] = $avatar['ContractorProfile']['company'];
			$i++;
		}
		shuffle($data);
		$data = array_slice($data, 0, 5);
		return $data;
	}
}
?>
